package com.ruoyi.uitity;

import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

//@Component
public class CustomMessageConverter implements MessageConverter {

    private final SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();

    @Override
    public Object fromMessage(org.springframework.amqp.core.Message message) {
        byte[] body = message.getBody();
        String content = new String(body, StandardCharsets.UTF_8);

        // 如果消息不是 JSON 格式，进行特殊处理
        if (content.matches("\\d+\\+\\d+")) { // 匹配类似 "1+14" 的表达式
            return content;
        }

        // 否则使用默认的 JSON 转换器
        return simpleMessageConverter.fromMessage(message);
    }

    @Override
    public org.springframework.amqp.core.Message toMessage(Object object, org.springframework.amqp.core.MessageProperties messageProperties) {
        return simpleMessageConverter.toMessage(object, messageProperties);
    }
}
